

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "gui.h"

#include "views.h"

#include "xapp_clock.h"
#include "xapp_factory_test.h"

enum tagFactoryTest_HRBpm
{
    tagFactoryTest_HRBpm_OK,
    tagFactoryTest_HRBpm_NG,
};

GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag ); 
static bool in_factorytest_hr_bmp=false;
void set_factorytest_hr_bmp_state(bool state)
{
    in_factorytest_hr_bmp=state;
}
bool is_in_factorytest_hr_bmp(void)
{
    return in_factorytest_hr_bmp;
}

static uint8_t* string_bpm = NULL;

const static ui_widgets_type_t wglFactoryTest_Heartrate_Measure[] = 
{
    [0] = uiCanvas( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),

    [1] = uiLabel( 50, 20, 180, 31, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_BACKCOLOR, ENCODING_ASCII, FONT_ASCII_1_24_31, "Heartrate", 9 ),

    // x 
    [2] = uiNumberWithBuffer(50, 70, GUI_LCD_RES_H, 26, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "BPM: %d", NULL, 10 ),

    // OK
    [3] = uiLabel(30, 150, 80, 40, COLOR_GREEN, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_BACKCOLOR, ENCODING_ASCII, FONT_ASCII_1_20_26, "OK", 2 ),
    // NG 
    [4] = uiLabel( 130, 150, 80, 40, COLOR_RED, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_BACKCOLOR, ENCODING_ASCII, FONT_ASCII_1_20_26, "NG", 2 ),

    //NOT TOUCH
    [5] = uiLabel( 50, 106, 180, 26, COLOR_BLACK, COLOR_YELLOW, ALIGN_LEFT_TOP, FILL_BACKCOLOR, ENCODING_ASCII, FONT_ASCII_1_20_26, "NOT TOUCH", 9 ),
    //TOUCH
    [6] = uiLabel( 50, 106, 180, 26, COLOR_BLACK, COLOR_BLUE, ALIGN_LEFT_TOP, FILL_BACKCOLOR, ENCODING_ASCII, FONT_ASCII_1_20_26, "TOUCH", 5 ),
};

const static ui_refresh_part_t refreshPartHeartrateBpm[] = 
{
    [0] = xRECTANGLE( 50, 70, GUI_LCD_RES_H, 26 ),
};

const static ui_widgets_type_t wglTouch[] = 
{
    [0] = uiTouch( 0, 150, 120, 80, tagFactoryTest_HRBpm_OK),
    [1] = uiTouch( 120, 150, 120, 80, tagFactoryTest_HRBpm_NG ),
};

GUI_VIEW_DEF_NR( viewFactoryTest_Heartrate_Measure ) = 
{
    // name 
    .name = "FactoryTest+HRBPM",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglFactoryTest_Heartrate_Measure ,
    .widget_cnt = WIDGET_COUNT( wglFactoryTest_Heartrate_Measure ),

    .flag = GUI_VIEW_FLAG_NONE
};

//static uint32_t m_idle_timestamp;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0
// 指定生命周期，生命周期结束后自动调用 destroy， 测试过程中，可以将Lifetime = 0 使屏幕常亮 
*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 300;
    menu->lifetime = 0; 

    string_bpm = gui_malloc(10);
    set_factorytest_hr_bmp_state(true);
    xapp_factory_test_set( FACTORY_TEST_ACTION_START_HRS_HB_MEASURE );
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
    set_factorytest_hr_bmp_state(false);
    xapp_factory_test_set( FACTORY_TEST_ACTION_STOP_HRS_HB_MEASURE );
    gui_free( string_bpm );    
    return GUI_DESTROY_RET_DEFAULT; 
}


/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;

    if( IS_REQ_SRC_CREATE( menu->source ) )
    {
        gui_widget_update_prop( &widgets[1], NULL );

        gui_widget_update_prop( &widgets[3], NULL );
        gui_widget_update_prop( &widgets[4], NULL );
        
        gui_widget_update_prop( &widgets[5], NULL );
        gui_widget_update_prop( &widgets[6], NULL );

        widgets[2].uiNumber.content = string_bpm;

        gui_part_t part = GUI_PART_CONFIG(refreshPartHeartrateBpm, 1000 );
        gui_part_create( &part );
    }
    

    // 更新心率显示
    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_PART )
    {
        const xapp_factorytest_desc_t* p_ft_desc = xapp_factory_test_get_desc();

        prop.number.value = p_ft_desc->param.hrhb.bpm;
        gui_widget_update_prop( &widgets[2], &prop );
        
        if(p_ft_desc->param.hrhb.not_touch)
        {
            widgets[5].visible = true;
            widgets[6].visible = false;
        }
        else
        {
            widgets[5].visible = false;
            widgets[6].visible = true;  
        }
    }


}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_CLICK:
        VIEW_CHECK_TOUCH( wglTouch, touch->x, touch->y, local_view_event_handler ); 
        break;
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    (void)pvParameters; 
}

static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    (void)pvParameters;
}

static void local_view_event_handler( uint32_t tag )
{
    switch( tag )
    {
        case tagFactoryTest_HRBpm_OK:
        gui_view_create( &viewFactoryTest_TP_CLICK );
        break;

        case tagFactoryTest_HRBpm_NG:
        gui_view_create( &viewFactoryTest_TP_CLICK );
        break;
    }
}
